import { api } from '@/api'

const useUserGroupStore = defineStore(
    // 唯一ID
    'userGroup',
    {
        state: () => ({

        }),
        getters: {

        },
        actions: {
            // 删除用户组
            deleteGroup(id) {
                return new Promise((resolve, reject) => {
                    api.delete(`user-groups/${id}`, {
                    }).then(res => {
                        resolve(res)
                    }).catch(error => {
                        reject(error)
                    })
                })
            },
            // 获取人员信息
            getGroup(params) {
                return new Promise((resolve, reject) => {
                    api.get('user-groups', {
                        params
                    }).then(res => {
                        resolve(res)
                    }).catch(error => {
                        reject(error)
                    })
                })
            },
            // 新增用户组
            addGroup(data) {
                return new Promise(resolve => {
                    api.post('user-groups', {
                        name: data.name,
                        parentId: data.parentId
                    }, {
                        // baseURL: '/mock/'
                    }).then(() => {
                        resolve()
                    })
                })
            },
            // 更新用户组
            editGroup(params) {
                return new Promise((resolve, reject) => {
                    api.put(`user-groups/${params.id}/info`, {
                        name: params.name,
                        parentId: params.parentId
                    }).then(res => {
                        resolve(res)
                    }).catch(error => {
                        reject(error)
                    })
                })
            },
            // 获取用户数
            getGroupTree() {
                return new Promise((resolve, reject) => {
                    api.get(`user-groups/trees`, {}).then(res => {
                        resolve(res)
                    }).catch(error => {
                        reject(error)
                    })
                })
            }
        }
    }
)

export default useUserGroupStore
